package com.carpedil.movie;

import com.carpedil.movie.service.MovService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;

/** @author Administrator */
@SpringBootApplication(scanBasePackages = {"com.carpedil.movie"})
@MapperScan("com.carpedil.movie.mapper")
public class MovieMain {
  public static void main(String[] args) {
    SpringApplication.run(MovieMain.class, args);
  }

  @Autowired RestTemplate restTemplate;
  @Autowired MovService movService;

  @RabbitListener(queues = "OrderQueue")
  public void getOrderInfo(String orderInfo) {
    if (orderInfo != null) {
      String movieName = orderInfo.substring(orderInfo.lastIndexOf(":") + 1);
      boolean b = movService.updateStorage(movieName);
      if (b) {
        try {
          Thread.sleep(15000);
          String url = "http://localhost:8003/success/" + movieName;
          String txt = restTemplate.getForEntity(url, String.class).getBody();
          System.out.println(txt + " " + orderInfo);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
}
